package com.nbjtjc.safe.api.emg.goods;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgGoodsLogMapper;
import com.nbjtjc.safe.mapper.emg.EmgGoodsMapper;
import com.nbjtjc.safe.model.emg.EmgGoodsLog;
import com.nbjtjc.safe.service.emg.EmgGoodsLogService;
import com.nbjtjc.safe.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 物资操作日志
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/emgGoodsLog")
public class GoodsLogController extends AbstractController<EmgGoodsLog> {

    @Autowired
    private EmgGoodsMapper emgGoodsMapper;
    @Autowired
    private EmgGoodsLogMapper emgGoodsLogMapper;

    @Autowired
    private FileUtil fileUtil;

    @Autowired
    private EmgGoodsLogService emgGoodsLogService;

    @SysLog("添加一次操作记录")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(EmgGoodsLog emgGoodsLog){
        Result result=new Result();
        try {
            result=emgGoodsLogService.add(emgGoodsLog);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }

        return result;
    }

    @SysLog("更新一次操作记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(EmgGoodsLog emgGoodsLog){
        Result result=new Result();
        try {
            result=emgGoodsLogService.update(emgGoodsLog);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @Override
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @SysLog("删除操作记录")
    public Result delete(int id) {
        Result result=new Result();
        try {
            result=emgGoodsLogService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @Override
    @RequestMapping(value = "/find",method = RequestMethod.POST)
    public Result find(EmgGoodsLog emgGoodsLog, Page page) {
        Result result=new Result();
        try {
            PageHelper.startPage(page.getPageCurrent(),page.getPageSize());
            List<EmgGoodsLog> emgGoodsLogs=emgGoodsLogMapper.search(emgGoodsLog,page);
            emgGoodsLogs.forEach(log->{
                if(log.getOperationType().equals(0)){
                    if(CollectionUtil.isNotEmpty(log.getEntryLogs())){
                        log.getEntryLogs().forEach(entryLog->{
                            log.setEntryAll(log.getEntryAll()+entryLog.getCount());
                            log.setLossAll(log.getLossAll()+entryLog.getLoss());
                        });
                    }
                }
            });
            result.setData(new PageInfo<>(emgGoodsLogs));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @PostMapping("/downGoodsLogFile")
    public  void getGoodsLogFile(HttpServletResponse response,Page page,EmgGoodsLog emgGoodsLog) throws IOException {
        fileUtil.writeFile(response,emgGoodsLogService.downGoodsLogFile(page,emgGoodsLog));
    }
}
